<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Forward declaration</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Forward_declaration"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Forward_declaration rootpage-Forward_declaration skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Forward declaration</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>forward declaration</b> is a <a href="Declaration_(computer_science)" class="mw-redirect" title="Declaration (computer science)">declaration</a> of an <a href="Identifier_(computer_programming)" class="mw-redirect" title="Identifier (computer programming)">identifier</a> (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete <a href="Definition" title="Definition">definition</a>.
</p><p>It is required for a <a href="Compiler" title="Compiler">compiler</a> to know certain properties of an identifier (size for <a href="Memory_allocation" class="mw-redirect" title="Memory allocation">memory allocation</a>, <a href="Data_type" title="Data type">data type</a> for type checking, such as <a href="Type_signature" title="Type signature">type signature</a> of functions), but it isn't required to know some other details, like the particular value it holds (in case of variables or constants) or definition (in the case of functions). This is particularly useful for <a href="One-pass_compiler" title="One-pass compiler">one-pass compilers</a> and separate compilation.
</p><p>Forward declaration is used in languages that require declaration before use; it is necessary for <a href="Mutual_recursion" title="Mutual recursion">mutual recursion</a> in such languages, as it is impossible to define such functions (or data structures) without a forward reference in one definition: one of the functions (respectively, data structures) must be declared first. It is also useful to allow flexible code organization, for example if one wishes to place the main body at the top, and called functions below it.
</p><p>In other languages forward declarations are not necessary, which generally requires instead a <a href="Multi-pass_compiler" title="Multi-pass compiler">multi-pass compiler</a> and for some compilation to be deferred to <a href="Link_time" title="Link time">link time</a>. In these cases identifiers must be defined (variables initialized, functions defined) before they can be employed during runtime without the need for pre-definition in the source code for either compilation or interpretation: identifiers do not need to be immediately resolved to an existing entity.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<p>A basic example in C is:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">printThisInteger</span><span class="p">(</span><span class="kt">int</span><span class="p">);</span>
</pre></div>
<p>In <a href="C_(programming_language)" title="C (programming language)">C</a> and <a href="C%2B%2B" title="C++">C++</a>, the line above represents a forward declaration of a <a href="Subroutine" class="mw-redirect" title="Subroutine">function</a> and is the <a href="Function_prototype" title="Function prototype">function's prototype</a>. After processing this declaration, the <a href="Compiler" title="Compiler">compiler</a> would allow the program code to refer to the entity <code>printThisInteger</code> in the rest of the program. The definition for a function must be provided somewhere (same file or other, where it would be the responsibility of the linker to correctly match references to a particular function in one or several object files with the definition, which must be unique, in another):
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">printThisInteger</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>Variables may have only forward declaration and lack definition. During compilation time these are initialized by language specific rules (to undefined values, 0, NULL pointers, ...). Variables that are defined in other source/object files must have a forward declaration specified with a keyword <code>extern</code>:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span><span class="w"> </span><span class="c1">//foo might be defined somewhere in this file</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bar</span><span class="p">;</span><span class="w"> </span><span class="c1">//bar must be defined in some other file</span>
</pre></div>
<p>In <a href="Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a> and other <a href="Niklaus_Wirth" title="Niklaus Wirth">Wirth</a> programming languages, it is a general rule that all entities must be declared before use, and thus forward declaration is necessary for mutual recursion, for instance. In C, the same general rule applies, but with an exception for undeclared functions and incomplete types. Thus, in C it is possible (although unwise) to implement a pair of <a href="Mutual_recursion" title="Mutual recursion">mutually recursive</a> functions thus:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">first</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">second</span><span class="p">(</span><span class="n">x</span><span class="mi">-1</span><span class="p">);</span><span class="w"> </span><span class="c1">// forward reference to second</span>
<span class="p">}</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">second</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">first</span><span class="p">(</span><span class="n">x</span><span class="mi">-1</span><span class="p">);</span><span class="w"> </span><span class="c1">// backward reference to first</span>
<span class="p">}</span>
</pre></div>
<p>In Pascal, the same implementation requires a forward declaration of <code>second</code> to precede its use in <code>first</code>. Without the forward declaration, the compiler will produce an error message indicating that the <a href="Identifier" title="Identifier">identifier</a> <code>second</code> has been used without being declared.
</p>
<div class="mw-heading mw-heading2"><h2 id="Classes">Classes</h2></div>
<p>In some object-oriented languages like <a href="C%2B%2B" title="C++">C++</a> and <a href="Objective-C" title="Objective-C">Objective-C</a>, it is sometimes necessary to forward-declare classes. This is done in situations when it is necessary to know that the name of the class is a type, but where it is unnecessary to know the structure.
</p><p>In C++, classes and structs can be forward-declared like this:
</p>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">MyClass</span><span class="p">;</span>
<span class="k">struct</span><span class="w"> </span><span class="nc">MyStruct</span><span class="p">;</span>
</pre></div>
<p>In C++, classes can be forward-declared if you only need to use the pointer-to-that-class type (since all object pointers are the same size, and this is what the compiler cares about). This is especially useful inside class definitions, e.g. if a class contains a member that is a pointer (or a reference) to another class.
</p><p>Forward-declaration is used to avoid unnecessary coupling which help reducing compilation time by reducing the number of header inclusion. This has a triple advantage:
</p>
<ul><li>reduce the number of files opened by #include (hence the number of operating system calls)</li>
<li>reducing the volume of the pre-processed files (as the header is not included)</li>
<li>reducing recompilation impact when the forward declared class is modified.</li></ul>
<p>Forward declaration of a class is not sufficient if you need to use the actual class type, for example, if you have a member whose type is that class directly (not a pointer), or if you need to use it as a base class, or if you need to use the methods of the class in a method.
</p><p>In Objective-C, classes and protocols can be forward-declared like this:
</p>
<div class="mw-highlight mw-highlight-lang-objc mw-content-ltr" dir="ltr"><pre><span class="k">@class</span> <span class="bp">MyClass</span>;
<span class="k">@protocol</span> <span class="nc">MyProtocol</span>;
</pre></div>
<p>In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. <style data-mw-deduplicate="TemplateStyles:r886049734">
/* start https://en.wikipedia.org/ */
.mw-parser-output .monospaced{font-family:monospace,monospace}
/* end https://en.wikipedia.org/ */
</style><span class="monospaced">MyClass *</span> or <span class="monospaced">id<MyProtocol></span>. This is especially useful inside class definitions, e.g. if a class contains a member that is a pointer to another class; to avoid <a href="Circular_reference" title="Circular reference">circular references</a> (i.e. that class might also contain a member that is a pointer to this class), we simply forward-declare the classes instead.
</p><p>Forward declaration of a class or protocol is not sufficient if you need to subclass that class or implement that protocol.
</p>
<div class="mw-heading mw-heading2"><h2 id="Forward_reference">Forward reference</h2></div>
<p>The term <b>forward reference</b> is sometimes used as a synonym of <i>forward declaration</i>.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> However, more often it is taken to refer to the actual <i>use</i> of an entity before any declaration; that is, the first reference to <code>second</code> in the code above is a forward reference.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> Thus, we may say that because forward declarations are mandatory in Pascal, forward <i>references</i> are prohibited.
</p><p>An example of (valid) forward reference in <a href="C%2B%2B" title="C++">C++</a>:
</p>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">C</span><span class="w"> </span><span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">mutator</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">myValue</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">accessor</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">myValue</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
<span class="k">private</span><span class="o">:</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">myValue</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
<p>In this example, there are two references to <code>myValue</code> before it is declared. C++ generally prohibits forward references, but they are allowed in the special case of class members. Since the member function <code>accessor</code> cannot be compiled until the compiler knows the type of the <a href="Member_variable" title="Member variable">member variable</a> <code>myValue</code>, it is the compiler's responsibility to remember the definition of <code>accessor</code> until it sees <code>myValue</code>'s declaration.
</p><p>Permitting forward references can greatly increase the complexity and memory requirements of a compiler, and generally prevents the compiler from being implemented in <a href="One-pass_compiler" title="One-pass compiler">one pass</a>.
</p>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://msdn2.microsoft.com/en-us/library/15k227ta(VS.71).aspx">MSDN: Converting to a Forward-Reference Class Type</a></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external free" href="http://pages.cs.wisc.edu/~fischer/cs536.s07/lectures/Lecture25.4up.pdf">http://pages.cs.wisc.edu/~fischer/cs536.s07/lectures/Lecture25.4up.pdf</a> </span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.fi.muni.cz/usr/jkucera/tic/tic0103.html">Thinking in C++: Inlines & the compiler</a></span>
</li>
</ol></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-04-22" href="https://en.wikipedia.org/wiki/?title=Forward_declaration&oldid=1286913658">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>